package chencb.stu.thrift.server2.config;

import chencb.stu.thrift.interfaces.IUserInter;
import chencb.stu.thrift.server2.rpcImpl.IUserInterImpl;
import org.apache.thrift.TMultiplexedProcessor;
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TThreadedSelectorServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TNonblockingServerTransport;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ThriftServerConfig implements InitializingBean {
    @Value("${thrift.server.port}")
    private int port;
    @Value("${thrift.server.minWorkerThreads}")
    private int minThreads;
    @Value("${thrift.server.maxWorkerThreads}")
    private int maxThreads;

    @Autowired
    private IUserInterImpl userInterImpl;

    /**
     * 非阻塞式
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("-----------------------------哈哈哈哈，哇哈哈哈");

        TNonblockingServerSocket transport = new TNonblockingServerSocket(7940);
        TThreadedSelectorServer.Args args = new TThreadedSelectorServer.Args(transport);

        TMultiplexedProcessor processor = new TMultiplexedProcessor();
        IUserInter.Processor datapoolServer = new IUserInter.Processor<IUserInter.Iface>(userInterImpl);
        processor.registerProcessor(IUserInter.class.getName(), datapoolServer);

        TProcessorFactory processorFactory = new TProcessorFactory(processor);
        args.processorFactory(processorFactory);
        args.transportFactory(new TFramedTransport.Factory());
        args.protocolFactory(new TBinaryProtocol.Factory());

        TThreadedSelectorServer server = new TThreadedSelectorServer(args);
        server.serve();
    }
}
